package com.approval.approvalapp.center.acitivty;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.MyAppliaction;
import com.approval.approvalapp.R;
import com.approval.approvalapp.center.adapter.ThingsCensusAdapter;
import com.approval.approvalapp.center.adapter.ThingsCensusDetailAdapter;
import com.approval.approvalapp.center.bean.ThingsCensusBean;
import com.approval.approvalapp.center.bean.ThingsCensusDetailBean;
import com.approval.approvalapp.center.bean.ThingsProductBean;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//事项详情
public class ThingsCensusDetailActivity extends BaseActivity {

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    List<ThingsCensusDetailBean> mList = new ArrayList<>();
    private ThingsCensusBean provinceCensusBean;
    private ThingsCensusDetailAdapter mAdapter;

    public static void goInto(Context context, String region_code, String type) {
        Intent intent = new Intent();
        intent.setClass(context, ThingsCensusDetailActivity.class);
        intent.putExtra("region_code", region_code);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.things_census_detail_layout;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ThingsCensusDetailAdapter(mList, this);
        mRecyclerView.setAdapter(mAdapter);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getData();
                    }
                }, 500);
            }
        });
        getData();
    }

    public void getData() {
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("org_code", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.ORG_CODE, "-1"));
        jsonObject.put("region_code", getIntent().getStringExtra("region_code"));
        String url1 = "v1/user/get_sxpt_nums";//升级
        String url2 = "v1/user/get_sxpt_num";//全省
        String url = "";
        if (getIntent().getStringExtra("type").equals("1")){
            //升级
            url = url1;
        }else {
            url = url2;//全省
        }
        NetRequsetUtil.getInstance().netRequestPostJson(url, jsonObject.toString(), new NetDownResponse() {
            public String TAG = getClass().getSimpleName();

            @Override
            public void success(String str) {
                Gson gson = new Gson();
                Type type = new TypeToken<List<ThingsCensusDetailBean>>() {
                }.getType();
                List<ThingsCensusDetailBean> list = gson.fromJson(str, type);
                mList.clear();
                mList.addAll(list);
                mAdapter.notifyDataSetChanged();

            }

            @Override
            public void errowithresponse(String str) {

            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {
                if (swipeRefreshLayout != null) {
                    swipeRefreshLayout.setRefreshing(false);
                }
            }
        });

    }


    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }
}
